//
//  HROrderApplyInfoViewController.m
//  haierapp
//
//  Created by gaotangzhang on 14-8-19.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "HROrderApplyInfoViewController.h"
#import "HROrderApplyCell.h"
#import "HROrderApplyHeader.h"
#import "HROrderApplyFooter.h"
#import "HROrderApplyInfoCell.h"
#import "HROrderApplyInfoImageCell.h"

#import "HROrderApplyInfo.h"

#define HEIGHT_HEADER_TABLEVIEW 40.0f
#define HEIGHT_FOOTER_TABLEVIEW 10.0f

#define HEIGHT_CELL_ORDER_SECTION 80.0f

#define HEIGHT_CELL_STATUS_SECTION 55.0f

#define HEIGHT_CELL_APPLYINFO 30.0f
#define HEIGHT_CELL_APPLYINFO_IMAGE 100.0f

#define IDENTIFIER_CELL_ORDER @"orderapplycell"



@interface HROrderApplyInfoViewController (){
    NSDictionary *_orderData;
    NSDictionary *_applyInfoData;
    
    HROrderApplyInfo *_orderInfo;
}

@end

@implementation HROrderApplyInfoViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    //去除tableview多余的间隔线
    UIView *footerView=[[UIView alloc]init];
    footerView.backgroundColor=[UIColor clearColor];
    self.tableView.tableFooterView=footerView;
    
    self.title=@"售后订单详情";
    
    [self loadOrderInfoData];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Methods

//加载售后订单详情
-(void)loadOrderInfoData{
    
    [self showCustomLoadingHud:kLoading inView:nil];
    
    [HRInterfaceManager getOrderApplyDetailById:_serviceNo completion:^(BOOL isSucceed, NSString *message, id data) {
        
        [self hideCustomLoadingHud];
        
        if (isSucceed) {
            _orderInfo=(HROrderApplyInfo *)data;
            
            [self.tableView reloadData];
        }else{
            [self showTextHUDInWindow:message];
            
            //清空页面
            _orderInfo=nil;
            
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    }];
    
}

-(UITableViewCell *)orderGoodsTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *cellIndentifier=IDENTIFIER_CELL_ORDER;
    
    HROrderApplyCell *cell=(HROrderApplyCell *)[tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    
    if (cell==nil) {
        cell=[[HROrderApplyCell alloc]init];
    }
    
    HROrderProductInfo *product=[_orderInfo.productInfo objectAtIndex:indexPath.row];
    
    [cell configWithData:product];
    
    
    return cell;
    
}

-(UITableViewCell *)statusInfoTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellIdentifier;
    UITableViewCell *cell;
    
    BOOL status=[[_orderData objectForKey:@"status"] boolValue];
    
    NSDictionary *statusInfo=[_orderData objectForKey:@"statusInfo"];
    
    if (status) {
        //退款成功
        cellIdentifier=@"statusInfoSucces";
        cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        
        if (cell==nil) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
            cell.textLabel.font=[UIFont fontWithName:FONT_NAME_DEFAULT size:12.0f];
            cell.detailTextLabel.font=[UIFont fontWithName:FONT_NAME_DEFAULT size:12.0f];
        }
        
        NSString *amount=[statusInfo objectForKey:@"amount"];
        NSString *date=[statusInfo objectForKey:@"date"];
        
        cell.textLabel.text=[NSString stringWithFormat:@"退款金额:%@元",amount];
        cell.detailTextLabel.text=[NSString stringWithFormat:@"退款时间:%@",date];
    }else{
        //正在处理
        cellIdentifier=@"statusInfoDealing";
        cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        
        if (cell==nil) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
            cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.font=[UIFont fontWithName:FONT_NAME_DEFAULT size:12.0f];
            cell.detailTextLabel.font=[UIFont fontWithName:FONT_NAME_DEFAULT size:12.0f];
            
            
            
        }
        
        NSString *desc=[statusInfo objectForKey:@"desc"];
        NSString *date=[statusInfo objectForKey:@"date"];
        cell.imageView.image=[UIImage imageNamed:@"order_ico"];
        
        cell.textLabel.text=desc;
        cell.detailTextLabel.text=date;
        
    }
    
    
    return cell;
}

-(UITableViewCell *)appInfoTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HRServiceInfo *serviceInfo=_orderInfo.serviceInfo;
    
    
    NSString *cellIdentifier;
    
    
    if (indexPath.row==5) {
        cellIdentifier=@"orderapplyinfoimagecell";
        HROrderApplyInfoImageCell *cell=(HROrderApplyInfoImageCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        
        if (cell==nil) {
            cell=[[HROrderApplyInfoImageCell alloc]init];
        }
        
        for (UIImageView *imageView in cell.imgViews) {
            
        }
        
        return cell;
    }else{
        cellIdentifier=@"orderapplyinfocell";
        HROrderApplyInfoCell *cell=(HROrderApplyInfoCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell==nil) {
            cell=[[HROrderApplyInfoCell alloc]init];
        }
        
        switch (indexPath.row) {
            case 0:
                cell.infoTitleLabel.text=@"服务类型:";
                cell.infoDetailLabel.text=serviceInfo.serviceType;
                break;
            case 1:
                cell.infoTitleLabel.text=@"问题描述:";
                cell.infoDetailLabel.text=serviceInfo.remakr;
                break;
            case 2:
                cell.infoTitleLabel.text=@"收货地址:";
                cell.infoDetailLabel.text=serviceInfo.address;
                break;
            case 3:
                cell.infoTitleLabel.text=@"联系人:";
                cell.infoDetailLabel.text=serviceInfo.customer;
                break;
            case 4:
                cell.infoTitleLabel.text=@"电话:";
                cell.infoDetailLabel.text=serviceInfo.phone;
                break;
                
            default:
                break;
        }
        
        return cell;
    }
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    if (_orderInfo==nil) {
        return 0;
    }else{
        return 2;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    if (section==0) {
        
        int count=0;
        
        
        if (_orderInfo.productInfo!=nil && [_orderInfo.productInfo count]>0) {
            count=[_orderInfo.productInfo count];
        }
        
        return count;
    }else{
        
        if (_orderInfo.serviceInfo!=nil) {
            return 5;
        }else{
            return 0;
        }
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell ;
    NSString *cellIndentifier;
    
    if (indexPath.section==0) {
        cell=[self orderGoodsTableView:tableView cellForRowAtIndexPath:indexPath];
    }else{
        cell=[self appInfoTableView:tableView cellForRowAtIndexPath:indexPath];
    }
    
    
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    HROrderApplyHeader *headerView=[[HROrderApplyHeader alloc]initWithType:APPLY_HEADER_TITLE_DESC showTopLine:YES];
    
    if(section==0){
        headerView.titleLabel.text=[NSString stringWithFormat:@"服务号:%@",_serviceNo];
        headerView.descLabel.text=_orderInfo.state;
    }else{
        headerView.titleLabel.text=@"服务单明细";
        headerView.descLabel.text=@"";
    }
    
    return headerView;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    if (section==0) {
        HROrderApplyFooter *footerView=[[HROrderApplyFooter alloc]initWithShowBottomLine:YES AndShowSeperateView:YES];
       
        HROrderProductInfo *productInfo=[_orderInfo.productInfo objectAtIndex:0];
        int count=1;
        if (productInfo.goodsAmount!=nil) {
            count=[productInfo.goodsAmount intValue];
        }
        
        CGFloat price=[productInfo.unitPrice floatValue] * count;
        
        footerView.goodsCountLabel.text=[NSString stringWithFormat:@"共%d件商品",count];
        footerView.goodsTotalPriceLabel.text=[NSString stringWithFormat:@"￥%.2f",price];
       
        return footerView;
    }else{
        UIView *footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
        footerView.backgroundColor=[UIColor colorFromHexRGB:@"ECECEC"];
        return footerView;
    }
    
}




-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return HEIGHT_CELL_ORDER_SECTION;
    }else if(indexPath.section==1 && indexPath.row==5){
        return HEIGHT_CELL_APPLYINFO_IMAGE;
    }else{
        return HEIGHT_CELL_APPLYINFO;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return HEIGHT_HEADER_TABLEVIEW;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (section==0) {
        return HEIGHT_FOOTER_ORDERCELL_SHOW_SEPERATE;
    }else{
        return HEIGHT_FOOTER_TABLEVIEW;
    }
    
}


#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}


@end
